package 二分算法;

/**
 * 二分查找: https://leetcode.cn/problems/binary-search/description/
 */
public class likou704 {


    // 暴力枚举是 O(n).  二分算法是 O(logN)
    public int twoSearch(int[] nums, int target) {
        // 查找区间的左端点. <t,  >= t
        int left = 0, right = nums.length - 1;
        while(left < right) {
            int mid = left + (right - left) / 2; //  左端点不加1
            if(nums[mid] < target) left = mid + 1;
            else right = mid;
        }
        // left = right 就是答案. 根据题意: 不一定有答案
        if(nums[left] == target) return left;
        return -1;
    }


}
